package com.atcapp.server.controller;

import com.atcapp.server.dao.StudentDAO;
import com.atcapp.server.dao.TeacherDAO;
import com.atcapp.server.repository.UserStuRepository;
import com.atcapp.server.repository.UserTeaRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {
    @Autowired
    private UserStuRepository userStuRepository;
    @Autowired
    private UserTeaRepository userTeaRepository;
    /*
    * 学生登录模块
    * */
    @PostMapping(value = "/Loginstu")
    public StudentDAO userStuLogin(@RequestParam("username")String userID,
                                @RequestParam("password")String password,
                                @RequestParam("permission")Integer permission) {

        if(permission==1){
        StudentDAO user = userStuRepository.findBystudentID(userID);
        if (user != null){

            if(user.getPassword().equals(password)) {
                return user;
            }
        }}

        return null;
    }
    /*
    * 教师登录模块
    * */
    @PostMapping(value = "/Logintea")
    public TeacherDAO userTeaLogin(@RequestParam("username")String userID,
                                @RequestParam("password")String password,
                                @RequestParam("permission")Integer permission) {


        if (permission == 2) {
            TeacherDAO user = userTeaRepository.findByteacherID(userID);
            if (user != null) {

                if (user.getPassword().equals(password)) {
                    return user;
                }
            }
        }
        return null;
    }
}
